package messy.test;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 
 */

/**
 * 日期工具
 * @author admin
 *
 */
public final class DateUtil {

	/**
	 * 将日期字符串转换为统一的日期对象
	 * @author jiaqi.han
	 * @date 2020年12月29日
	 */
	public static final Date changeToTheSameDateFormat(String dateStr) {
		// 替换
		dateStr = dateStr.replace("-", "/");
		Date data = new Date();
		SimpleDateFormat format1;
		SimpleDateFormat format2;
		if (dateStr.length() == 10) {
			format1 = new SimpleDateFormat("yyyy/MM/dd");
			format2 = new SimpleDateFormat("MM/dd/yyyy");
		} else if (dateStr.length() == 16) {
			int counts = dateStr.length() - dateStr.replace("/", "").length();
			if (counts == 2) {
				format1 = new SimpleDateFormat("yyyy/MM/dd HH:mm");
				format2 = new SimpleDateFormat("MM/dd/yyyy HH:mm");
			} else {
				format1 = new SimpleDateFormat("yyyy/MM/dd HH/mm");
				format2 = new SimpleDateFormat("MM/dd/yyyy HH/mm");
			}
		} else {
			format1 = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
			format2 = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
		}
		// 看第三位是不是为'/'
		if (dateStr.indexOf("/") != 2) {
			try {
				data = format1.parse(dateStr);
				return data;
			} catch (Exception e) {

			}
		} else {
			try {
				data = format2.parse(dateStr);
				return data;
			} catch (Exception e) {

			}
		}
		return null;
	}
}
